<?php

class TranslateForm extends CFormModel
{
	public $language, $source, $target;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return NGWordForm the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return new $className;
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('source, target, language', 'safe'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
		);
	}

	public function save() {
		$language=$this->language;
		$category='app';
		
		$messageFile = Yii::app()->messages->basePath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$category.'.php';
		$dict = include($messageFile);
		$dict[trim($this->source)] = trim($this->target);
		$data = "<?php return ".var_export($dict, true).";";
		file_put_contents($messageFile, $data);
	}
	
	public function getLanguageListData() {
		return array(
			'en'=>'English',
			'ja'=>'Japanese',
		);
	}
	
	public function getCurrentLanguage() {
		$lang = 'ja';
		$cookie_lang = !empty(Yii::app()->request->cookies['lang']) ? Yii::app()->request->cookies['lang']->value : null;
		if ($cookie_lang)
			$lang = $cookie_lang;
		
		if (isset($_GET['lang'])) {
			$lang = $_GET['lang'];
			Yii::app()->request->cookies['lang'] = new CHttpCookie('lang', $lang);
		}
		return $lang;
	}
}